import React from 'react';
import PropTypes from 'prop-types';
import { Router } from 'dva/router';
// import { showLoading, hideLoading } from 'react-redux-loading-bar';

const cached = {};
const registerModel = (app, model) => {
  if (!cached[model.namespace]) {
    app.model(model);
    cached[model.namespace] = 1;
  }
};

const Routers = ({ history, app }) => {
  // const handleChildRoute = ({ location, params, view }) => {
  //   console.log(location, params, view);
  // };

  const routes = [
    {
      path: '/',
      getComponent(nextState, cb) {
        require.ensure([], (require) => {
          registerModel(app, require('../../controller/app'));
          cb(null, require('../../view/app/'));
        }, 'app');
      },
      childRoutes: [
        {
          path: '/dashboard',
          breadcrumbName: '控制台',
          breadcrumbIcon: 'desktop',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('../../controller/dashboard'));
              cb(null, require('../../view/dashboard/'));
            }, 'dashboard');
          },
        },
        {
          path: '/user',
          breadcrumbName: '用户管理',
          breadcrumbIcon: 'exception',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('../../controller/user'));
              cb(null, require('../../view/user/'));
            }, 'user');
          },
        },
      ],
    },
  ];
  const createElement = (Component, props) => {
    return <Component {...props} key={props.location.pathname} />;
  };
  return <Router createElement={createElement} history={history} routes={routes} />;
};

Routers.propTypes = {
  history: PropTypes.object,
  app: PropTypes.object,
};

export default Routers;
